﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using StyleMVVM.DependencyInjection;

namespace StyleMVVM.MVC.Extensions
{
	public class InjectedFilterAttributeFilterProvider : FilterAttributeFilterProvider
	{
		private IDependencyInjectionContainer container;

		public InjectedFilterAttributeFilterProvider(IDependencyInjectionContainer container)
		{
			this.container = container;
		}

		protected override IEnumerable<FilterAttribute> GetActionAttributes(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
		{
			List<FilterAttribute> returnValue = new List<FilterAttribute>();

			foreach (FilterAttribute actionAttribute in base.GetActionAttributes(controllerContext, actionDescriptor))
			{
				container.Inject(actionAttribute);

				returnValue.Add(actionAttribute);
			}

			return returnValue;
		}

		protected override IEnumerable<FilterAttribute> GetControllerAttributes(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
		{
			List<FilterAttribute> returnValue = new List<FilterAttribute>();

			foreach (FilterAttribute controllerAttribute in base.GetControllerAttributes(controllerContext, actionDescriptor))
			{
				container.Inject(controllerAttribute);

				returnValue.Add(controllerAttribute);
			}

			return returnValue;
		}
	}
}
